package com.example.huwang.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.huwang.R;
import com.example.huwang.model.CaseBean;
import com.example.huwang.model.DefinitionBean;
import com.huwang.greendao.gen.CaseBeanDao;
import com.huwang.greendao.gen.DefinitionBeanDao;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.example.huwang.app.MainApplication.dbController;

/**
 * Created by 张柱 on 2022/4/11
 * 诈骗类型详情页面
 */
public class CrimeTypeDetailActivity extends BaseActivity {
    @BindView(R.id.id_tv_content)
    public TextView tvContent;
    @BindView(R.id.id_recycle_view)
    public RecyclerView recycleView;

    @BindView(R.id.title)
    public TextView title;

    @BindView(R.id.return_text)
    public TextView returnText;
    @BindView(R.id.return_btn)
    public ImageView returnBtn;


    private BaseQuickAdapter<CaseBean, BaseViewHolder> mAdapter;
    private List<CaseBean> data = new ArrayList();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_crime_detail);
        ButterKnife.bind(this);
        returnText.setOnClickListener(v -> finish());
        returnBtn.setOnClickListener(v -> finish());
        initRecycleView();
    }

    private void initRecycleView() {
        Intent intent = getIntent();
        int type = 1;
        if (intent != null) {
            type = intent.getIntExtra("type", 1);
        }
        List<DefinitionBean> definitionBeans = dbController.getDefinitionBeanDao().queryBuilder().where(DefinitionBeanDao.Properties.Type.eq(type)).list();
        List<CaseBean> beans = dbController.getCaseBeanDao().queryBuilder().where(CaseBeanDao.Properties.Type.eq(type)).list();
        title.setText(definitionBeans.get(0).getTitle());
        tvContent.setText(definitionBeans.get(0).getDefinition());
        data.clear();
        data.addAll(beans);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        recycleView.setLayoutManager(manager);
        recycleView.setAdapter(mAdapter = new BaseQuickAdapter<CaseBean, BaseViewHolder>(R.layout.item_layout_example, data) {

            @Override
            protected void convert(BaseViewHolder helper, CaseBean item) {
                helper.setText(R.id.id_rv_title, item.getTitle());
                helper.setText(R.id.id_tv_content, item.getContent());
                helper.addOnClickListener(R.id.more);
            }
        });
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                int id = view.getId();
                switch (id){
                    case R.id.more:
//                        Toast.makeText(CrimeTypeDetailActivity.this,"更多",Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(CrimeTypeDetailActivity.this, MoreDetailActivity.class);
                        intent.putExtra("title",data.get(position).getTitle());
                        intent.putExtra("content",data.get(position).getContent());
                        intent.putExtra("pic_num",data.get(position).getId()-1);
                        startActivity(intent);
                        break;
                }
            }
        });
    }
}
